//
//  BaseViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/2/8.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "BaseViewController.h"
@interface BaseViewController ()<UITextFieldDelegate,UIGestureRecognizerDelegate>
{
    int     _lastPosition2;                     // 记录上次滑动的位置
    BOOL    _isDecelerate;                      // 是否减速
    BOOL    _isDrag;                            //是否滑动
}

@property (retain,nonatomic) UIView* emptyDataBackgroundView;
@property (retain,nonatomic) UIImageView* logo;
@property (retain,nonatomic) UILabel* titleLabel;
@property (retain,nonatomic) UILabel* desLabel;

@end
@implementation BaseViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor clearColor];
    [self.navigationController.navigationBar setBackgroundImage:[UIColor createImageWithColor:[UIColor colorWithHexString:@"fafafa"]] forBarMetrics:UIBarMetricsDefault];
   self.view.backgroundColor=[UIColor colorWithHexString:@"efeff4"];
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

+ (id)getController:(NSString *)className withData:(id)data
{
    Class clazz = nil;
    BaseViewController *controller = nil;
    
    if(className && (clazz = NSClassFromString(className))) {
        controller = [[NSClassFromString(className) alloc] init];
    } else {
        controller = [[BaseViewController alloc] init];
    }
    controller.data = data;
    
    return controller;
}

- (void)setNaviMiddleButton: (NSString* )title {
    CGSize textSize = [title sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:18], NSFontAttributeName, nil]];
    UIView* backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 12, 44)];
    
    // 按钮
    UIButton* middleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    middleButton.frame = CGRectMake(0, 0, textSize.width, 44);
    [middleButton setTitle:title forState:UIControlStateNormal];
    [middleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    middleButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:18];
    [middleButton addTarget:self action:@selector(clickMiddleButton:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:middleButton];
    
    // 图标
    UIImageView* arrowImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navi_img_arrow_down"]];
    arrowImage.frame = CGRectMake(textSize.width + 3, 0, 9, 5);
    arrowImage.center = CGPointMake(arrowImage.center.x, backgroundView.center.y);
    [backgroundView addSubview:arrowImage];
    
    self.navigationItem.titleView = backgroundView;
}

- (void)clickMiddleButton:(id)sender
{
    
}

- (void)setNaviTitle:(NSString* )title {
    self.title = title;
    NSShadow *shadowAttribute = [[NSShadow alloc]init];
    shadowAttribute.shadowColor = [UIColor clearColor];
    shadowAttribute.shadowOffset = CGSizeMake(0, -1);
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor colorWithHexString:@"0000000"], NSForegroundColorAttributeName,
                                                                     shadowAttribute, NSShadowAttributeName,
                                                                     [UIFont boldSystemFontOfSize:18], NSFontAttributeName,
                                                                     nil]];
}

- (void)setNaviTitle:(NSString* )title color:(UIColor*)color {
    self.title = title;
    NSShadow *shadowAttribute = [[NSShadow alloc]init];
    shadowAttribute.shadowColor = [UIColor clearColor];
    shadowAttribute.shadowOffset = CGSizeMake(0, -1);
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     color, NSForegroundColorAttributeName,
                                                                     shadowAttribute, NSShadowAttributeName,
                                                                     [UIFont boldSystemFontOfSize:20], NSFontAttributeName,
                                                                     nil]];
}

- (void)setNaviTitle:(NSString* )title subTitle:(NSString* )subTitle {
    [self setNaviTitle:title subTitle:subTitle subColor:Color666666];
}


-(BOOL)judgeChineseOrEnglish:(NSString *)title{
    for(int i=0; i< [title length];i++){
        int a = [title characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)
        {
            return YES;
        }
        
    }
    return NO;
}




- (void)setNaviLeftButton {
    [self setNaviLeftButton:@"返回"];
}
- (void)setNaviLeftButton:(NSString* )title
{
    UIImage * lpImage = [UIImage imageNamed:@"nav_back.png"];
    UIImage * lpImage2 = [UIImage imageByApplyingAlpha:0.5 image:lpImage];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    [btn setImage:lpImage forState:UIControlStateNormal];
    [btn setImage:lpImage2 forState:UIControlStateHighlighted];
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);
    [btn addTarget:self action:@selector(clickLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = backBtn;
}
- (void)setNaviLeftImgButton:(NSString* )imgName
{
    UIImage * lpImage = [UIImage imageNamed:imgName];
    UIImage * lpImage2 = [UIImage imageByApplyingAlpha:0.5 image:lpImage];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
    [btn setImage:lpImage forState:UIControlStateNormal];
    [btn setImage:lpImage2 forState:UIControlStateHighlighted];
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, -60, 0, 0);
    [btn addTarget:self action:@selector(clickLeftImgButton:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = backBtn;
}

- (void)setRightButtonAtMinRect:(NSString*)title
{
    
    if (IS_NOT_EMPTY(title)) {
        CGSize textSize = [title sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.0f], NSFontAttributeName, nil]];
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame = CGRectMake(0, 0, textSize.width+20, textSize.height);
        [rightButton setTitle:title forState:UIControlStateNormal];
        [rightButton setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -30)];
        rightButton.titleLabel.font=[UIFont systemFontOfSize:15];
        UIView *backBtnView = [[UIView alloc] initWithFrame:rightButton.bounds];
        [rightButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
        [backBtnView addSubview:rightButton];
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtnView];
        [self.navigationItem setRightBarButtonItem:rightButtonItem animated:YES];
    }else{
        self.navigationItem.rightBarButtonItem=nil;
    }
}

- (void)setNaviRightButton:(NSString* )title
{
    [self setNaviRightButton:title color:Color2b98f6];
}

- (void)setNaviRightButton:(NSString* )title color:(UIColor*)color
{
    if (IS_NOT_EMPTY(title)) {
        CGSize textSize = [title sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.0f], NSFontAttributeName, nil]];
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightButton setTitleColor: color forState:UIControlStateNormal];
        rightButton.frame = CGRectMake(0, 0, textSize.width+40, textSize.height+10);
        [rightButton setTitle:title forState:UIControlStateNormal];
        [rightButton setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -40)];
        rightButton.titleLabel.font=[UIFont systemFontOfSize:15];
        [rightButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        [self.navigationItem setRightBarButtonItem:rightButtonItem animated:YES];
    }else{
        self.navigationItem.rightBarButtonItem=nil;
    }
}

- (void)setNaviRightButton:(NSString* )title action:(BOOL)action
{
    if (IS_NOT_EMPTY(title)) {
        
        if(self.navigationItem.rightBarButtonItem){
            
            UIButton *rightButton = (UIButton *)self.navigationItem.rightBarButtonItem.customView;
            [rightButton setUserInteractionEnabled:action];
            if (action) {
                [rightButton setTitleColor: Color2b98f6 forState:UIControlStateNormal];
            }else{
                [rightButton setTitleColor: [UIColor grayColor] forState:UIControlStateNormal];
            }
        }else{
            
            CGSize textSize = [title sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.0f], NSFontAttributeName, nil]];
            UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
            rightButton.frame = CGRectMake(0, 0, textSize.width+40, textSize.height+10);
            [rightButton setTitle:title forState:UIControlStateNormal];
            [rightButton setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -40)];
            rightButton.titleLabel.font=[UIFont systemFontOfSize:15];
            [rightButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
            [rightButton setUserInteractionEnabled:action];
            if (action) {
                
                [rightButton setTitleColor: Color2b98f6 forState:UIControlStateNormal];
            }else{
                [rightButton setTitleColor: [UIColor grayColor] forState:UIControlStateNormal];
            }
            UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
            [self.navigationItem setRightBarButtonItem:rightButtonItem animated:YES];
        }
    }else{
        self.navigationItem.rightBarButtonItem=nil;
    }
}

- (void)setNaviRightImgButton:(NSString* )imgName
{
    if (IS_NOT_EMPTY(imgName)) {
        UIImage *img1 = [UIImage imageNamed:imgName];
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame = CGRectMake(0, 10, 20, 20);
        [rightButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
        [rightButton setImage:img1 forState:UIControlStateNormal];
        UIImage * lpImage2 = [UIImage imageByApplyingAlpha:0.5 image:img1];
        [rightButton setImage:lpImage2 forState:UIControlStateHighlighted];
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        [self.navigationItem setRightBarButtonItem:rightButtonItem animated:YES];
    }
}

- (void)setNaviRightImgButton:(NSString* )imgName frame:(CGRect)frame
{
    if (IS_NOT_EMPTY(imgName)) {
        UIImage *img1 = [UIImage imageNamed:imgName];
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame = frame;
        [rightButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
        [rightButton setImage:img1 forState:UIControlStateNormal];
        UIImage * lpImage2 = [UIImage imageByApplyingAlpha:0.5 image:img1];
        [rightButton setImage:lpImage2 forState:UIControlStateHighlighted];
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        [self.navigationItem setRightBarButtonItem:rightButtonItem animated:YES];
    }
}
- (void)clickRightButton:(id)sender {
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"DonotdisturbViewController" object:nil];
    
}

-(void)clickLeftButton:(UIButton*)sender
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"DonotdisturbBack" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)clickLeftImgButton:(UIButton*)sender
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"clickLeftImgButton" object:nil];
}
- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
-(void) viewWillDisappear:(BOOL)animation {
    [super viewWillDisappear:animation];
    
}
- (void)handlePanelHighlight:(id)sender{
    [[(UIView*)sender subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[UIButton class]]) {
            [obj setHighlighted:[(UIButton*)sender isHighlighted]];
        }
    }];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

#ifdef __IPHONE_7_0
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden
{
    return NO;
}
#endif

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

- (BOOL)shouldAutorotate
{
    return YES;
}

#pragma mark
#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    _isDecelerate = YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int currentPostion = scrollView.contentOffset.y;
    
    if (_isDrag) {
        if ((abs(_lastPosition2 - currentPostion) < 3 && _isDecelerate) || abs(_lastPosition2 - currentPostion) < 2) { // 快速滑动减速时或者手指慢速移动时, 恢复图片下载线程
            _isDrag = NO;
        }else {
        }
    }else{
        
    }
    
    _lastPosition2 = currentPostion;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _isDecelerate = NO;
    _isDrag = YES;
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _isDrag = NO;
}


@end
